package de.softwareforge.published.taglet;

/*
 * Copyright (C) 2007-2008 Henning P. Schmiedehausen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Map;

import com.sun.javadoc.Doc;
import com.sun.javadoc.Tag;

import com.sun.tools.doclets.Taglet;
import com.sun.tools.doclets.internal.toolkit.Configuration;
import com.sun.tools.doclets.standard.Standard;

import de.softwareforge.published.doclet.PublishedDoclet;
import de.softwareforge.published.util.Locale;
import de.softwareforge.published.util.LocaleMsg;
import de.softwareforge.published.util.PublishInformation;
import de.softwareforge.published.util.Utils;

import static de.softwareforge.published.util.LocaleMsg.MSG_HEADER;


/**
 * This {@link Taglet} evaluates the <code>api.level</code> javadoc tags and generates the publishing information in the classes.
 *
 * @author <a href="mailto:henning@schmiedehausen.org">Henning P. Schmiedehausen</a>
 * @version $Id: PublishedTaglet.java 97 2008-02-24 11:24:34Z hps@intermeta.de $
 */
public class PublishedTaglet
        implements Taglet
{

    /**
     * The name of the javadoc tag.
     *
     * @api.level unpublished
     */
    public static final String NAME = "@api.level";

    private final Locale messages;

    /**
     * Register the taglet with the Javadoc application.
     *
     * @param tagletMap Map of taglets available.
     */
    public static void register(final Map<String, Taglet> tagletMap)
    {
        PublishedTaglet taglet = new PublishedTaglet();

        if (tagletMap.get(taglet.getName()) != null)
        {
            tagletMap.remove(taglet.getName());
        }

        tagletMap.put(taglet.getName(), taglet);
    }

    /**
     * Creates a new PublishedTaglet object.
     */
    private PublishedTaglet()
    {
        Configuration conf = Standard.htmlDoclet.configuration();
        messages = new Locale(PublishedDoclet.LOCALE_RESOURCES, conf.root);
    }

    /**
     * Return the name of the Taglet.
     *
     * @return The name of the taglet.
     */
    public String getName()
    {
        return NAME.substring(1);
    }

    /**
     * Indicates that this taglet might be present in constructor documentation blocks.
     *
     * @return True value.
     */
    public boolean inConstructor()
    {
        return true;
    }

    /**
     * Indicates that this taglet might be present in field documentation blocks.
     *
     * @return True value.
     */
    public boolean inField()
    {
        return true;
    }

    /**
     * Indicates that this taglet might be present in method documentation blocks.
     *
     * @return True value.
     */
    public boolean inMethod()
    {
        return true;
    }

    /**
     * @see Taglet#inOverview()
     */
    public boolean inOverview()
    {
        return true;
    }

    /**
     * @see Taglet#inPackage()
     */
    public boolean inPackage()
    {
        return true;
    }

    /**
     * @see Taglet#inType()
     */
    public boolean inType()
    {
        return true;
    }

    /**
     * @see Taglet#isInlineTag()
     */
    public boolean isInlineTag()
    {
        return false;
    }

    /**
     * @see Taglet#toString(Tag)
     */
    public String toString(final Tag tag)
    {
        return toString(new Tag[] { tag });
    }

    /**
     * @see Taglet#toString(Tag[])
     */
    public String toString(Tag[] tags)
    {
        String[] fields = Utils.processTags(tags, messages);

        if (fields == null)
        {
            return null;
        }

        Doc holderDocument = tags[0].holder();
        PublishInformation publishInformation = Utils.getPublishInformation(fields, holderDocument.name(), messages);

        if (publishInformation == null)
        {
            return null;
        }

        String holderType = messages.getText(LocaleMsg.valueOf("MSG_TYPE_" + Utils.findHolderType(holderDocument).toUpperCase()));
        StringBuffer sb = new StringBuffer("<DT><B>").append(messages.getText(MSG_HEADER)).append("</B><DD>\n");
        sb.append("<table cellpadding=2 cellspacing=0><tr><td style=\"").append(publishInformation.getStyle()).append("\">");
        sb.append(messages.getText(LocaleMsg.valueOf("MSG_PUBLISH_" + publishInformation.getKey().toUpperCase()), holderType));
        sb.append("</td></tr>\n</table>\n");

        return sb.toString();
    }
}
